Imports System
Imports System.Runtime.InteropServices
Imports System.Windows.Interop

Namespace Aero

    Public Class VistaAeroAPI

#Region " API Declarations "

        <DllImport("dwmapi.dll", CharSet:=CharSet.Auto)> _
        Private Shared Sub DwmExtendFrameIntoClientArea(ByVal hWnd As System.IntPtr, ByRef pMargins As MARGIN_STRUCT)
        End Sub

        <DllImport("dwmapi.dll", CharSet:=CharSet.Auto)> _
        Private Shared Sub DwmIsCompositionEnabled(ByRef IsIt As Boolean)
        End Sub

#End Region

#Region " Private Declarations "

        Public Structure MARGIN_STRUCT

            Dim Left As Integer
            Dim Right As Integer
            Dim Top As Integer
            Dim Bottom As Integer

            Sub New(ByVal intLeft As Integer, ByVal intRight As Integer, ByVal intTop As Integer, ByVal intBottom As Integer)
                Left = intLeft
                Right = intRight
                Top = intTop
                Bottom = intBottom
            End Sub

            Sub New(ByVal t As Thickness)
                Left = CType(t.Left, Integer)
                Right = CType(t.Right, Integer)
                Top = CType(t.Top, Integer)
                Bottom = CType(t.Bottom, Integer)
            End Sub

        End Structure

#End Region

#Region " Methods "

        ''' <summary>
        ''' Makes any WPF window a full Aero Glass window
        ''' </summary>
        ''' <param name="window">Pass the window object.  (Me)</param>
        ''' <returns>Boolan</returns>
        ''' <remarks>Very cool</remarks>
        Public Shared Function ExtendGlassFrame(ByVal window As Window) As Boolean
            Return ExtendGlassFrame(window, New Thickness(-1))
        End Function

        ''' <summary>
        ''' Makes any WPF window an Aero Glass window
        ''' </summary>
        ''' <param name="window">Pass the window object.  (Me)</param>
        ''' <param name="margin">Set margins to extend the glass into or to make the window all glass pass : New Thickness(-1)</param>
        ''' <returns>Boolan</returns>
        ''' <remarks>Very cool</remarks>
        Public Shared Function ExtendGlassFrame(ByVal window As Window, ByVal margin As Thickness) As Boolean

            If Not IsDwmIsCompositionEnabled() Then
                Return False
            End If

            Dim hwnd As IntPtr = New WindowInteropHelper(window).Handle

            If hwnd = IntPtr.Zero Then
                Throw New InvalidOperationException("The Window must be shown before extending glass.")
            End If

            window.Background = Brushes.Transparent
            HwndSource.FromHwnd(hwnd).CompositionTarget.BackgroundColor = Colors.Transparent

            Dim margins As New MARGIN_STRUCT(margin)
            DwmExtendFrameIntoClientArea(hwnd, margins)
            Return True
        End Function

        ''' <summary>
        ''' Checks if DWM Composition is enabled.
        ''' </summary>
        Public Shared Function IsDwmIsCompositionEnabled() As Boolean

            Dim bolResult As Boolean
            DwmIsCompositionEnabled(bolResult)
            Return bolResult
        End Function

#End Region

    End Class

End Namespace
